package bb.lanxing.lib.devices.ble.device;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.base.SimpleBluetoothGattCallbackCompat;
import bb.lanxing.lib.devices.core.ble.AbsBleDevice;
import bb.lanxing.lib.devices.core.provider.DataNotifier;
import java.util.UUID;
import android.util.Log;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import bb.lanxing.lib.devices.ble.BLEAttributes;

public class StandardRowerDevice extends AbsBleDevice {
    final static private String TAG = "StandardRowerDevice";
    final static private boolean DebugFTMS = false;
    static private int intValueAvgStrokeRate = 0 ;
    static private double doubleValueTotalDistance = 0;
    static private double doubleValuePace = 0;
    static private double doubleValueAvgPace = 0;
    static private int intValuePower = 0;
    static private int intValueAvgPower = 0;
    static private int intValueResistanceLevel = 0;

    public StandardRowerDevice(Context context, SmartDevice smartDevice) {
        super(smartDevice);
        init(context, smartDevice.getAddress(), new RowerBluetoothGattCallback(this));
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onServicesDiscovered() {
        super.onServicesDiscovered();
        BluetoothGattCharacteristic characteristic = mBluetoothGatt.getService(UUID.fromString(BLEAttributes.BLE_FTMS_SERVICE)).getCharacteristic(UUID.fromString(BLEAttributes.BLE_FTMS_CHAR_ROWER_DATA));
        mBluetoothGatt.readCharacteristic(characteristic);
        setCharacteristicNotification(characteristic, true);
        if(DebugFTMS){
            //do nothing,just debug
        }else{
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException unused1) {
            }
            writeDeviceName00(); 
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException unused2) {
            }
            writeDeviceName07();
        }
    }
    private static class RowerBluetoothGattCallback extends SimpleBluetoothGattCallbackCompat<StandardRowerDevice> {
        RowerBluetoothGattCallback(StandardRowerDevice standardRowerDevice) {
            super(standardRowerDevice);
        }

        public int unsignedByteToInt(byte b) {
            return b & 0xFF;
        }

        public int unsignedBytesToInt24(byte b0, byte b1, byte b2) {
            return (unsignedByteToInt(b0) + (unsignedByteToInt(b1) << 8))
                    + (unsignedByteToInt(b2) << 16) ;
        }
        @Override
        public void onCharacteristicChanged(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            int intValueFlag1byte = 0;
            int intValueFlag2byte = 0;
            int intValueStrokeRate = 0;
            int intValueStrokeCount = 0;
            int intValueEnergy = 0;

            super.onCharacteristicChanged(bluetoothGatt, bluetoothGattCharacteristic);
            UUID uuid = bluetoothGattCharacteristic.getUuid();
            int type = ((StandardRowerDevice) this.mClientRef).getType();

            if (UUID.fromString(BLEAttributes.BLE_FTMS_CHAR_ROWER_DATA).equals(uuid)) {
                intValueFlag1byte = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
                intValueFlag2byte = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                if(DebugFTMS) {
                    intValueStrokeRate = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1))/2;
                    intValueStrokeCount = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                    intValueAvgStrokeRate = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1))/2;
                    doubleValueTotalDistance = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1))*1000).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    doubleValuePace = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1))/60).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                    intValueResistanceLevel = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1)/10;
                    intValueEnergy = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                    intValueAvgPower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                    DataNotifier.notifyRowerData(type, ((StandardRowerDevice) this.mClientRef).getAddress(),intValueStrokeRate,intValueStrokeCount,intValueAvgStrokeRate, doubleValueTotalDistance,doubleValuePace,intValuePower,intValueResistanceLevel,intValueEnergy);
                }else if((intValueFlag1byte == 174) && (intValueFlag2byte == 11)) {  //0xAE 0x0B rower DKN-RW700-00066
                    intValueStrokeRate = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 2))/2;
                    intValueStrokeCount = (int)bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 3);
                    intValueAvgStrokeRate = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 5))/2;
                    byte[] value = bluetoothGattCharacteristic.getValue();
                    doubleValueTotalDistance = new BigDecimal((float)(unsignedBytesToInt24(value[6],value[7],value[8]))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    doubleValuePace = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 9))/30).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 11);
                    intValueResistanceLevel = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 13));
                    intValueEnergy = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 15));
                    Log.d(TAG,"intValueAvgStrokeRate is "+ intValueAvgStrokeRate);
                    Log.d(TAG,"doubleValueTotalDistance is "+ doubleValueTotalDistance);
                    Log.d(TAG,"doubleValuePace is "+ doubleValuePace);
                    Log.d(TAG,"intValuePower is "+ intValuePower);
                    Log.d(TAG,"intValueResistanceLevel is "+ intValueResistanceLevel);
                    DataNotifier.notifyRowerData(type, ((StandardRowerDevice) this.mClientRef).getAddress(),intValueStrokeRate,intValueStrokeCount,intValueAvgStrokeRate, doubleValueTotalDistance,doubleValuePace,intValuePower,intValueResistanceLevel,intValueEnergy);
                }else if((intValueFlag1byte == 255) && (intValueFlag2byte == 10)) {  //0xFF 0x0A rower Concept2 Part1
                    intValueAvgStrokeRate = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 2))/2;
                    byte[] value = bluetoothGattCharacteristic.getValue();
                    doubleValueTotalDistance = new BigDecimal((float)(unsignedBytesToInt24(value[3],value[4],value[5]))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    doubleValuePace = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 6))/30).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    doubleValueAvgPace = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 8))/30).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 10);
                    intValueAvgPower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 12);
                    intValueResistanceLevel = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 14));
                }else if((intValueFlag1byte == 0) && (intValueFlag2byte == 1)) {  //0x00 0x01 rower Concept2 Part2
                    intValueStrokeRate = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 2))/2;
                    intValueStrokeCount = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 3);
                    intValueEnergy = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 5));
                    DataNotifier.notifyRowerData(type, ((StandardRowerDevice) this.mClientRef).getAddress(),intValueStrokeRate,intValueStrokeCount,intValueAvgStrokeRate, doubleValueTotalDistance,doubleValuePace,intValuePower,intValueResistanceLevel,intValueEnergy);
                }
            }
        }
    }
}
